package com.fr.report.script.function;

import com.fr.report.script.Function;
import com.fr.report.script.Function.Type;
import com.fr.report.script.NormalFunction;
import com.fr.report.script.Primitive;
import com.fr.report.script.core.FArray;
import com.fr.report.script.core.FunctionHelper;

public class MIN extends NormalFunction
{
  public Object run(Object[] paramArrayOfObject)
  {
    if (paramArrayOfObject.length < 1)
      return Primitive.ERROR_NAME;
    double d = 1.7976931348623157E+308D;
    for (int i = 0; i < paramArrayOfObject.length; ++i)
    {
      if (paramArrayOfObject[i] == null)
        break label47:
      d = Math.min(d, parseObject(paramArrayOfObject[i]));
    }
    label47: return FunctionHelper.parsePrimitiveDouble(d);
  }

  public Function.Type getType()
  {
    return Function.MATH;
  }

  private double parseObject(Object paramObject)
  {
    if (paramObject instanceof Number)
      return ((Number)paramObject).doubleValue();
    if (paramObject instanceof Boolean)
      return ((((Boolean)paramObject).booleanValue()) ? 1.0D : 0.0D);
    if (paramObject instanceof FArray)
    {
      FArray localFArray = (FArray)paramObject;
      double d = 1.7976931348623157E+308D;
      int i = 0;
      int j = localFArray.length();
      while (i < j)
      {
        d = Math.min(d, parseObject(localFArray.elementAt(i)));
        ++i;
      }
      return d;
    }
    if (paramObject != null)
      try
      {
        return Double.parseDouble(paramObject.toString());
      }
      catch (NumberFormatException localNumberFormatException)
      {
        return 0.0D;
      }
    return 0.0D;
  }

  public String getCN()
  {
    return "MIN(number1,number2,…): 返回参数列表中的最小值。\nNumber1,number2,…:1到30个需要找出最小值的参数。\n备注:\n    若参数中没有数字，函数MIN将返回0。\n    参数应为数字、空白单元格、逻辑值或是表示数值的文本串。如果参数是错误值时，MIN将返回错误信息。\n    如果数组或引用参数中包含可解析文本值，逻辑值，零值或空白单元格，这些值都将参与计算，而不可解析的文本值忽略不计。\n示例:\n如果B1:B4包含3，6，9，12，则:\nMIN(B1:B4)等于3。\nMIN(B1:B4,0)等于0。";
  }

  public String getEN()
  {
    return "MIN(number1,number2,…): Returns the smallest number in a set of values.\nNumber1,number2,… are 1 to 30 numbers for which you want to find the minimum value.\n\nRemarks:\n1. If the arguments contain no numbers, MIN returns 0. \n2. You can specify arguments that are numbers, empty cells, logical values, or text representations of numbers. Arguments that are error values or text that cannot be translated into numbers cause errors. \n3. If an argument is an array or reference, only numbers in that array or reference are used. Empty cells, logical values, or text in the array or reference are ignored. \n\nExample:\n   If B1:B4 are 3, 6, 9, 12 then MIN(B1:B4)=3, MIN(B1:B4,0)=0.";
  }
}